package net.openl10n.mdf;


import net.openl10n.document.api.IReferencePart;

public class ReferencePart extends AbstractDocumentPart implements IReferencePart{

	private final ReferenceId reference;
	
	private final int hashCode;
	
	public ReferencePart(PartId id, Long version, ReferenceId reference) {
		super(id, version);
		if(reference == null)
			throw new IllegalArgumentException("reference cannot be null");
		this.reference = reference;
		this.hashCode = generateHashCode();
	}
	

	@Override
	public final ReferenceId getReference() {
		return reference;
	}
	
	@Override
	public final boolean equals(Object obj) {
		if(obj == null) return false;
		if( !(obj instanceof ReferencePart) ) return false;
		ReferencePart other = (ReferencePart) obj;
		return ( super.equals(other) 
				&& other.reference.equals(reference) );
	}
	
	@Override
	public final int hashCode() {
		return hashCode;
	}
	
	private int generateHashCode() {
	    int hash = 1;
	    hash = hash * 31 + super.hashCode();
	    hash = hash * 31 + reference.hashCode();
	    return hash;
	}
	
}
